package com.dataseek.iot.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.sys.entity.*;
import com.dataseek.iot.sys.mapper.MenuMapper;
import com.dataseek.iot.sys.mapper.RoleMapper;
import com.dataseek.iot.sys.mapper.RoleMenuMapper;
import com.dataseek.iot.sys.mapper.UserRoleMapper;
import com.dataseek.iot.sys.service.RoleService;
import com.dataseek.iot.sys.vo.RoleVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.util.SnowflakeIdWorker;
import com.dataseek.iot.toolkit.tenant.config.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @discription:
 * @author: Freya.Yan
 * @date: 2022/1/11
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {


    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public RoleVo queryRole(String roleName, String status, Integer num, Integer size) {
        List<RoleTable> mapList = new ArrayList<>();
        RoleVo RoleVo = new RoleVo();
        QueryWrapper<Role> qw = new QueryWrapper<>();
        if (roleName != null && !roleName.isEmpty()) {
            qw.like("role_name", roleName);
        }
        if (status != null && !status.isEmpty()) {
            qw.eq("status", status);
        }
        qw.orderByAsc("role_set");
        IPage<Role> page = new Page<>(num, size);
        List<Role> roleMapList = roleMapper.selectPage(page, qw).getRecords();
        for (int i = 0; i < roleMapList.size(); i++) {
            Role role = roleMapList.get(i);
            RoleTable roleTable = new RoleTable();
            roleTable.setRoleId(role.getRoleId());
            roleTable.setTenantCode(role.getTenantCode());
            roleTable.setRoleName(role.getRoleName());
            roleTable.setRoleSet(role.getRoleSet());
            roleTable.setRoleKey(role.getRoleKey());
            roleTable.setStatus(role.getStatus());
            roleTable.setRemark(role.getStatus());
            roleTable.setCreateBy(role.getCreateBy());
            roleTable.setUpdateBy(role.getUpdateBy());
            long roleId = role.getRoleId();
            QueryWrapper<RoleMenu> roleMenuQw = new QueryWrapper<>();
            roleMenuQw.eq("role_id", roleId);
            List<String> pathList = new ArrayList<>();
            List<RoleMenu> roleMenus = roleMenuMapper.selectList(roleMenuQw);
            for (int j = 0; j < roleMenus.size(); j++) {
                long menuId = roleMenus.get(j).getMenuId();
                QueryWrapper<Menu> menuQw = new QueryWrapper<>();
                menuQw.eq("menu_id", menuId);
                String path = menuMapper.selectOne(menuQw).getPath();
                pathList.add(path);
            }
            roleTable.setMenus(pathList);
            mapList.add(roleTable);
        }
        RoleVo.setPage(num);
        RoleVo.setSize(size);
        RoleVo.setTotal(page.getTotal());
        RoleVo.setList(mapList);
        return RoleVo;
    }

    @Override
    public Integer updateRole(RoleTable roleTable) {
        String tenantCode = DynamicDataSourceContextHolder.getDataSourceKey();
        Role role = new Role();
        role.setTenantCode(tenantCode);
        role.setRoleId(roleTable.getRoleId());
        role.setRoleName(roleTable.getRoleName());
        role.setRoleKey(roleTable.getRoleKey());
        role.setRoleSet(roleTable.getRoleSet());
        role.setRemark(roleTable.getRemark());
        int resultCode = 0;
        resultCode = roleMapper.updateById(role);
        long roleId = roleTable.getRoleId();
        List<String> menus = roleTable.getMenus();
        if (menus != null && menus.size()>0) {
            QueryWrapper<RoleMenu> qwRoleMenu = new QueryWrapper<>();
            qwRoleMenu.eq("role_id", roleId);
            roleMenuMapper.delete(qwRoleMenu);
            for (int i = 0; i < menus.size(); i++) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setTenantCode(tenantCode);
                roleMenu.setRoleId(roleId);
                String path = menus.get(i);
                QueryWrapper<Menu> qwMenu = new QueryWrapper<>();
                qwMenu.eq("path", path);
                long menuId = menuMapper.selectOne(qwMenu).getMenuId();
                roleMenu.setMenuId(menuId);
                resultCode = roleMenuMapper.insert(roleMenu);
            }
        }
        return resultCode;
    }

    @Override
    public Integer insertRole(RoleTable roleTable) {
        String tenantCode = DynamicDataSourceContextHolder.getDataSourceKey();
        Role role = new Role();
        int resultCode = 0;
        String roleName = roleTable.getRoleName();
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq("role_name", roleName);
        Integer count = roleMapper.selectCount(qw);
        if (count == 0) {
            role.setTenantCode(tenantCode);
            role.setRoleName(roleTable.getRoleName());
            role.setRoleKey(roleTable.getRoleKey());
            role.setRoleSet(roleTable.getRoleSet());
            role.setRemark(roleTable.getRemark());
            role.setCreateBy(roleTable.getCreateBy());
            role.setUpdateBy(roleTable.getUpdateBy());
            resultCode = roleMapper.insert(role);
            long roleId = roleMapper.selectOne(qw).getRoleId();
            List<String> menus = roleTable.getMenus();
            for (int i = 0; i < menus.size(); i++) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                String path = menus.get(i);
                QueryWrapper<Menu> qwMenu = new QueryWrapper<>();
                qwMenu.eq("path", path);
                long menuId = menuMapper.selectOne(qwMenu).getMenuId();
                roleMenu.setMenuId(menuId);
                roleMenu.setRelId(SnowflakeIdWorker.nextId());
                roleMenu.setTenantCode(tenantCode);
                resultCode = roleMenuMapper.insert(roleMenu);
            }
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;
    }

    @Override
    public Integer deleteRole(List<Integer> roleIds) {
        int resultCode = roleMapper.deleteBatchIds(roleIds);
        Integer count = 0;
        for (int i = 0; i < roleIds.size(); i++) {
            Integer id = roleIds.get(i);
            QueryWrapper<UserRole> qw = new QueryWrapper<>();
            qw.eq("role_id", id);
            count = count + userRoleMapper.selectCount(qw);
        }
        if (count > 0) {
            resultCode = RspStatusEnum.FAIL.getCode();
        } else {
            for (int i = 0; i < roleIds.size(); i++) {
                QueryWrapper<RoleMenu> qwRole = new QueryWrapper<>();
                long roleId = roleIds.get(i);
                qwRole.eq("role_id", roleId);
                resultCode = roleMenuMapper.delete(qwRole);
            }
        }

        return resultCode;
    }

}
